package com.baomidou.springwind.controller.supply;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.controller.NodeData;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.mapper.NetworkDeliveryDao;
import com.baomidou.springwind.mapper.OrderDataDao;
import com.baomidou.springwind.pojo.SiteCoverBuilder;
import com.baomidou.springwind.pojo.SiteSelection;
import com.baomidou.springwind.pojo.SiteSelectionBuilder;
import com.baomidou.springwind.service.CustomServiceService;
import com.baomidou.springwind.service.OrderDataService;
import com.baomidou.springwind.service.ProjectService;
import com.baomidou.springwind.utils.ParamMap;
import com.baomidou.springwind.utils.ZvalueUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.springwind.mapper.CustomServiceDao;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 物流供应链-库存持有成本
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class CustomServiceController extends BaseController {

    @Autowired
    private OrderDataService orderDataService;
    @Autowired
    private OrderDataDao orderDataDao;
    @Autowired
    private ProjectService projectService;
    /**
     * 供应链数据导入
     */
    @RequestMapping("/service/list")
    @Permission("1001")
    public String inventoryList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/custom_service_list", model);
    }

    /**
     * 库存持有成本数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/service/getData")
    @Permission("1001")
    public String getData(Page<OrderData> page, String batchId) {
        page = new Page<OrderData>(page.getCurrent(), page.getSize());
        EntityWrapper<OrderData> wrapper = new EntityWrapper<OrderData>();
        if (!StringUtils.isBlank(batchId)) {
            wrapper.eq("batch_id", batchId);
        }
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = orderDataService.selectPage(page, wrapper);
        List<OrderData> orderDataList = page.getRecords();
        if (orderDataList.size() > 0) {
            Project project = projectService.selectById(orderDataList.get(0).getBatchId());
            for (OrderData orderData : orderDataList) {
                orderData.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }

    /**
     * 客户分析主界面
     */
    @RequestMapping("/service")
    @Permission("1001")
    public String customGroup(Model model) {
        EntityWrapper<OrderData> wrapper = new EntityWrapper<OrderData>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.groupBy("branch_id");
        model.addAttribute("prjList", orderDataService.selectList(wrapper));
        return pjaxPath("/lpp/network/supply/service/main", model);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/service/bar1")
    public String getMap(Long batchId, Model model) {
        JSONObject ret = new JSONObject();
        Map<String, Object> pm = getParameterMap();
        if (pm.get("center").toString().length()<0||pm.get("center").toString()==""){
            pm.put("center",getCenter());
        }
        pm.put("batchId",getProjectId());
        pm.put("operatorId",getCurrentUserId());
        List<Map<String, Object>> s1=orderDataDao.selectDeliveryReliability(pm);
        List<Map<String, Object>> s2=orderDataDao.selectDeliveryReliabilityCounts2(pm);
        List<Map<String, Object>> s3=orderDataDao.selectOrderSuceess1(pm);
        List<Map<String, Object>> s4=orderDataDao.selectOutStock(pm);
        List<Map<String, Object>> s5=orderDataDao.selectOutStockRate(pm);
        if(s1.size()==0){
            String message="查询数据异常";
            return callbackFail(message);
        }
        JSONArray array1=new JSONArray();
        JSONArray array2=new JSONArray();
        JSONArray array3=new JSONArray();
        JSONArray array4=new JSONArray();
        JSONArray array5=new JSONArray();
        double sumCounts=0;
        for (int i=0;i<s4.size();i++){
            sumCounts=sumCounts+Double.valueOf(s4.get(i).get("orderQuantity").toString());
        }
        for (int i=0;i<s2.size();i++){
            array1.add(s1.get(i).get("secondCenter"));
            array2.add(Double.valueOf(s1.get(i).get("secondCounts").toString())/Double.valueOf(s2.get(i).get("secondCounts").toString()));
            double double3=0;
            for (int i1=0;i1<s3.size();i1++){
                if (s3.get(i1).get("delivery").toString().equals(s2.get(i).get("delivery").toString())){
                    double3=Double.valueOf(s3.get(i).get("secondCounts").toString())/Double.valueOf(s2.get(i).get("secondCounts").toString());
                }
            }
            array3.add(double3);
            array4.add((Double.valueOf(s4.get(i).get("orderQuantity").toString())-Double.valueOf(s4.get(i).get("signQuantity").toString()))/sumCounts);
            array5.add(Double.valueOf(s5.get(i).get("countsRow").toString())/Double.valueOf(s2.get(i).get("secondCounts").toString()));
        }
        ret.put("x1",array1);
        ret.put("x2",array2);
        ret.put("x3",array3);
        ret.put("x4",array4);
        ret.put("x5",array5);
        return callbackSuccess(ret);
    }
    public String getCenter(){
        List<Map<String, Object>> customService=orderDataDao.selectCenter(getProjectId());
        return customService.get(0).get("branch_id").toString();
    }
}
